<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/2/5
 * Time: 22:05
 */

namespace app\api\controller;


use app\api\Base2Controller;

class Fuaddress extends Base2Controller
{

    /**
     * 添加修改区域
     */
    public function addArea()
    {
        if (!($areaid = input('areaids')))
        {
            return json_fail_return('请正确输入信息');
        }
        $userid=$this->user_id;
        $areaArr = explode(',',$areaid);
        $area2 = model('area')->getAreaInfo(['area_id'=>$areaArr[0]]);
        $city = $area2['area_parent_id'];
        $area1 = model('area')->getAreaInfo(['area_id'=>$city]);
        $provice = $area1['area_parent_id'];
        $fuaddress = model('fuaddress');
        //查看是否存在,存在修改,不存在添加
        $result = $fuaddress->info(['fuwuid'=>$userid,'city'=>$city]);
        if (!$result) {
            $data = [
                'fuwuid' => $userid,
                'provice' => $provice,
                'city' => $city,
                'areaid' => $city . ',' . $areaid . ',',
                'status' => 1,
                'created_at' => date('Y-m-d H:i:s', time()),
                'updated_at' => date('Y-m-d H:i:s', time()),
            ];
            $bool = $fuaddress->add($data);
            if ($bool !== false)
            {
                return json_success_return([],'添加成功');
            }else{
                return json_fail_return('添加失败');
            }
        }else{
            $data = [
                'areaid' => ',' . $areaid . ',',
                'updated_at' => date('Y-m-d H:i:s', time()),
            ];
            $bool = $fuaddress->edit(['id'=>$result['id']],$data);
            if ($bool !== false)
            {
                return json_success_return([],'修改成功');
            }else{
                return json_fail_return('修改失败');
            }
        }
    }

    /**
     * 区域列表
     */
    public function areaList()
    {
        $userid = $this->user_id;
        $fuaddress = model('fuaddress');
        $pagesize = input('pagesize')?input('pagesize'):15;
        $page = input('page')?input('page'):1;
        $field = 'ds_fuwu_area.id,ds_fuwu_area.provice,ds_fuwu_area.city,a1.area_name as provicename,a2.area_name as cityname,ds_fuwu_area.areaid';
        $list = $fuaddress->getList(['ds_fuwu_area.fuwuid'=>$userid,'ds_fuwu_area.status'=>1],$field,$pagesize);
        foreach ($list as $key=>$value)
        {
            $ids = mb_substr(mb_substr($value['areaid'],1),0,-1);
            $areaArr = model('area')->getarea_list(['area_id'=>['in',$ids]],'area_id,area_name');
            $list[$key]['areaArr'] =$areaArr;
        }
      return json_success_return($list);
    }

    /**
     * 区域删除
     */
    public function areaDel()
    {
        if (!($areaid = input('areaid'))) {
            return json_fail_return('请正确输入信息');
        }
        $userid = $this->user_id;
//        1省级2城市级3区域级
        $sheep = input('sheep') ? input('sheep') : 3;
        $condition = [
            'fuwuid' => $userid,
        ];
        $fuaddress = model('fuaddress');
        if ($sheep == 3) {
            $condition['areaid'] = ['like', '%,' . $areaid . ',%'];
//            区域级修改
            $info = $fuaddress->info($condition, 'id,areaid');
            $aresstr = str_replace(',' . $areaid . ',', ',', $info['areaid']);
            $bool = $fuaddress->edit(['id' => $info['id']], ['areaid' => $aresstr]);
            if ($bool !== false) {
                return json_success_return([], '删除成功');
            } else {
                return json_fail_return('删除失败');
            }
        }
        if ($sheep == 1) {
            $condition['provice'] = $areaid;
        } elseif ($sheep == 2) {
            $condition['city'] = $areaid;
        }
        $bool = $fuaddress->del($condition);
        if ($bool) {
            return json_success_return([], '删除成功');
        } else {
            return json_fail_return('删除失败');
        }
    }


    /**
     * 获取省市区
     */
    public function get_area_list()
    {
        $areaid = input('param.areaid');
        
        $area_model = model('area');
         #获取手机顶级分类
        $where = array();
        $where['area_parent_id'] = $areaid;
        $field = 'area_id,area_name';
        $area_class = $area_model->getarea_list($where, $field);
        return json_success_return($area_class);
    }

}